package com.bestcem.xm.survey.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bestcem.xm.component.mybatis.service.impl.XmServiceImpl;
import com.bestcem.xm.survey.entity.SurveyRespondent;
import com.bestcem.xm.survey.mapper.SurveyRespondentMapper;
import com.bestcem.xm.survey.service.SurveyRespondentService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author guita
 * @description 针对表【ds_survey_respondent】的数据库操作Service实现
 * @createDate 2023-01-09 15:15:37
 */
@Service
@Slf4j
public class SurveyRespondentServiceImpl extends XmServiceImpl<SurveyRespondentMapper, SurveyRespondent>
        implements SurveyRespondentService {

    @Override
    public SurveyRespondent getOneRespondent(JSONObject params) {
        //蛇形会自动转成驼峰bean
        //project_id=》projectId
        LambdaQueryWrapper<SurveyRespondent> wrapper = new QueryWrapper<>(params.toJavaObject(SurveyRespondent.class)).lambda();
        //TODO JSON类型的需要自己写一下
        return baseMapper.selectOne(wrapper);
    }

    @Override
    public Long getDeliverRspdAmount(String deliverId, String groupId, List<String> groupIdList, String memberId,
                                     Integer status, List<String> dedCodeList, String projectId) {
        // 获取门店投放的成功完成数据
        /*Long amount;
        if (StringUtils.isNotEmpty(deliverId)) {
            amount = baseMapper.selectCount(new LambdaQueryWrapper<SurveyRespondent>()
                    .eq(SurveyRespondent::getGroupId, groupId)
                    .eq(SurveyRespondent::getDeliverId, deliverId)
                    .eq(SurveyRespondent::getStatus, status));
        } else if (StringUtils.isNotEmpty(memberId)) {
            amount = baseMapper.selectCount(new LambdaQueryWrapper<SurveyRespondent>()
                    .eq(SurveyRespondent::getGroupId, groupId)
                    .eq(SurveyRespondent::getMemberId, memberId)
                    .eq(SurveyRespondent::getStatus, status));
        } else if (CollectionUtils.isNotEmpty(groupIdList)) {
            amount = baseMapper.selectCount(new LambdaQueryWrapper<SurveyRespondent>()
                    .eq(SurveyRespondent::getGroupId, groupId)
                    .eq(SurveyRespondent::getStatus, status)
                    .in(SurveyRespondent::getGroupId, groupIdList));
        } else if (CollectionUtils.isNotEmpty(dedCodeList)) {
            amount = baseMapper.selectCount(new LambdaQueryWrapper<SurveyRespondent>()
                    .eq(SurveyRespondent::getGroupId, groupId)
                    .eq(SurveyRespondent::getStatus, status)
                    .in(SurveyRespondent::getDedCode, dedCodeList));

            if (amount != null && amount > 0) {
                amount = baseMapper.selectCount(new LambdaQueryWrapper<SurveyRespondent>()
                        .eq(SurveyRespondent::getGroupId, groupId)
                        .eq(SurveyRespondent::getStatus, status));
            }
        } else if (status != null) {
            amount = baseMapper.selectCount(new LambdaQueryWrapper<SurveyRespondent>()
                    .eq(SurveyRespondent::getGroupId, groupId)
                    .eq(SurveyRespondent::getStatus, status));
        } else {
            amount = baseMapper.selectCount(new LambdaQueryWrapper<SurveyRespondent>()
                    .eq(SurveyRespondent::getGroupId, groupId));
        }
        return amount;*/
        Long amount;
        if (StringUtils.isNotEmpty(groupId)) {
            amount = baseMapper.selectCount(new LambdaQueryWrapper<SurveyRespondent>()
                    .eq(SurveyRespondent::getProjectId, projectId)
                    .eq(SurveyRespondent::getGroupId, groupId)
                    .eq(SurveyRespondent::getDeliverId, deliverId)
                    .eq(SurveyRespondent::getStatus, status));
        } else if (StringUtils.isNotEmpty(memberId)) {
            amount = baseMapper.selectCount(new LambdaQueryWrapper<SurveyRespondent>()
                    .eq(SurveyRespondent::getProjectId, projectId)
                    .eq(SurveyRespondent::getDeliverId, deliverId)
                    //.eq(SurveyRespondent::getGroupId, groupId)
                    .eq(SurveyRespondent::getMemberId, memberId)
                    .eq(SurveyRespondent::getStatus, status));
        } else if (CollectionUtils.isNotEmpty(groupIdList)) {
            amount = baseMapper.selectCount(new LambdaQueryWrapper<SurveyRespondent>()
                    .eq(SurveyRespondent::getProjectId, projectId)
                    //.eq(SurveyRespondent::getGroupId, groupId)
                    .eq(SurveyRespondent::getStatus, status)
                    .eq(SurveyRespondent::getDeliverId, deliverId)
                    .in(SurveyRespondent::getGroupId, groupIdList));
        } else if (CollectionUtils.isNotEmpty(dedCodeList)) {
            amount = baseMapper.selectCount(new LambdaQueryWrapper<SurveyRespondent>()
                    //.eq(SurveyRespondent::getGroupId, groupId)
                    .eq(SurveyRespondent::getProjectId, projectId)
                    .eq(SurveyRespondent::getDeliverId, deliverId)
                    .eq(SurveyRespondent::getStatus, status)
                    .in(SurveyRespondent::getDedCode, dedCodeList));

            if (amount == 0) {
                amount = baseMapper.selectCount(new LambdaQueryWrapper<SurveyRespondent>()
                        //.eq(SurveyRespondent::getGroupId, groupId)
                        .eq(SurveyRespondent::getDeliverId, deliverId)
                        .eq(SurveyRespondent::getProjectId, projectId)
                        .eq(SurveyRespondent::getStatus, status));
            }
        } else if (status != null) {
            amount = baseMapper.selectCount(new LambdaQueryWrapper<SurveyRespondent>()
                    //.eq(SurveyRespondent::getGroupId, groupId)
                    .eq(SurveyRespondent::getDeliverId, deliverId)
                    .eq(SurveyRespondent::getProjectId, projectId)
                    .eq(SurveyRespondent::getStatus, status));
        } else {
            amount = baseMapper.selectCount(new LambdaQueryWrapper<SurveyRespondent>()
                    .eq(SurveyRespondent::getDeliverId, deliverId)
                    .eq(SurveyRespondent::getProjectId, projectId));
        }
        return amount;
    }

    @Override
    public String getDeliverRspdStatus(String deliverId, String memberId, String dedCode, Integer status, String projectId) {
        // 获取某投放的投放对象的答题状态
        String state = "";
        SurveyRespondent respondent;
        if (StringUtils.isNotEmpty(memberId)) {
            if (status != null) {
                respondent = baseMapper.selectOne(new LambdaQueryWrapper<SurveyRespondent>()
                        .eq(SurveyRespondent::getDeliverId, deliverId)
                        .eq(SurveyRespondent::getMemberId, memberId)
                        .eq(SurveyRespondent::getStatus, status)
                        .last("limit 1"));
            } else {
                respondent = baseMapper.selectOne(new LambdaQueryWrapper<SurveyRespondent>()
                        .eq(SurveyRespondent::getDeliverId, deliverId)
                        .eq(SurveyRespondent::getMemberId, memberId)
                        .last("limit 1"));
            }
        } else {
            if (status != null) {
                respondent = baseMapper.selectOne(new LambdaQueryWrapper<SurveyRespondent>()
                        .eq(SurveyRespondent::getDeliverId, deliverId)
                        .eq(SurveyRespondent::getDedCode, dedCode)
                        .eq(SurveyRespondent::getStatus, status)
                        .last("limit 1"));
            } else {
                respondent = baseMapper.selectOne(new LambdaQueryWrapper<SurveyRespondent>()
                        .eq(SurveyRespondent::getDeliverId, deliverId)
                        .eq(SurveyRespondent::getDedCode, dedCode)
                        .last("limit 1"));
            }
        }
        if (respondent != null) {
            return respondent.getStatus().toString();
        }
        return state;
    }
}




